package com.snail.billing.session;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import com.snail.billing.DataCache;
import com.snail.billing.net.BillingArrayHttpSession;
import com.snail.billing.net.BillingSecurity;

public class WorkContinueAskSession extends BillingArrayHttpSession {

	public WorkContinueAskSession(String workformId, String content) {
		DataCache cache = DataCache.getInstance();
		String host = cache.importParams.hostBusiness;
		String address = String.format("http://%s/problem/continueAsk.do", host);
		setAddress(address);
		addBillingPair("workformId", workformId);
		List<String> tempList = new ArrayList<String>();
		tempList.add("type");
		tempList.add("8");
		tempList.add("content");
		tempList.add(content);
		tempList.add("operatorId");
		tempList.add(cache.importParams.aid);
		tempList.add("operateDate");
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
		Calendar calendar = Calendar.getInstance();
		tempList.add(format.format(calendar.getTime()));
		tempList.add("relatedId");
		tempList.add(workformId);
		tempList.add("sourceType");
		tempList.add("1");
		tempList.add("isRead");
		tempList.add("0");
		//
		addBillingPair("json", bulidJson(tempList));
		buildParamPair();
	}

	@Override
	public BillingSecurity getSecurity() {
		if (DataCache.getInstance().isSandbox) {
			return BillingSecurity.securitySandbox2;
		}
		return BillingSecurity.security;
	}

	protected String bulidJson(List<String> paramsList) {
		if (paramsList == null || paramsList.size() == 0) {
			return "";
		}
		StringBuilder builder = new StringBuilder();
		builder.append("{");
		for (int i = 0; i < paramsList.size(); i += 2) {
			String key = paramsList.get(i);
			String value = paramsList.get(i + 1);
			if (i != 0) {
				builder.append(",");
			}
			builder.append("\"").append(key).append("\":\"").append(value).append("\"");
		}
		builder.append("}");
		return builder.toString();
	}

}
